<template>
  <div>
    <nav class="my-nav van-hairline--bottom">
      <a
        href="javascript:;"
        :class="{ active: sorter === 'weight_desc' }"
        @click="changeTab('weight_desc')"
        >推荐</a
      >
      <a
        href="javascript:;"
        :class="{ active: sorter == null }"
        @click="changeTab(null)"
        >最新</a
      >
      <div class="logo"><img src="@/assets/login.png" alt="" /></div>
    </nav>
    <van-pull-refresh
      @refresh="onRefresh"
      v-model="refreshing"
      success-text="刷新成功"
    >
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <CellListVue v-for="(item, index) in list" :key="index" :item="item" />
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { AxioscellList } from '@/api/user'
import CellListVue from '@/components/CellList.vue'
export default {
  name: 'article-page',
  components: {
    CellListVue
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      refreshing: false,
      page: 1,
      sorter: 'weight_desc'
    }
  },
  async created () {
    const {
      data: { rows }
    } = await AxioscellList()
    this.list = rows
  },
  methods: {
    async onLoad () {
      const res = await AxioscellList({
        current: this.page,
        pageSize: 10,
        sorter: this.sorter
      })
      this.page++
      this.list.push(...res.data.rows)
      this.loading = false

      if (this.list.length >= res.data.total) {
        this.finished = true
      }
      console.log(res)
    },
    onRefresh () {
      // this.page = 5
      this.onLoad()
      this.$toast('刷新成功')

      this.refreshing = false
    },
    changeTab (sorter) {
      // 把传递过来的状态值保存一下
      this.sorter = sorter
      // 页码变为1
      this.page = 1
      // 清空数组
      this.list = []
      this.loading = false
      // 无条件的手动把finished改为false，代表重新开始
      this.finished = false
      // 手动调用加载数据的方法
      this.onLoad()
    }
  }
}
</script>

<style lang="less" scoped>
.my-nav {
  height: 44px;
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  z-index: 999;
  background: #fff;
  display: flex;
  align-items: center;
  > a {
    color: #999;
    font-size: 14px;
    line-height: 44px;
    margin-left: 20px;
    position: relative;
    transition: all 0.3s;
    &::after {
      content: '';
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      bottom: 0;
      width: 0;
      height: 2px;
      background: #222;
      transition: all 0.3s;
    }
    &.active {
      color: #222;
      &::after {
        width: 14px;
      }
    }
  }
  .logo {
    flex: 1;
    display: flex;
    justify-content: flex-end;
    > img {
      width: 64px;
      height: 28px;
      display: block;
      margin-right: 10px;
    }
  }
}
</style>
